import { Router } from 'express';
import alertController from '@controllers/alert.controller';

const router = Router();

/**
 * Alert Routes
 */

/**
 * @swagger
 * /alerts:
 *   get:
 *     summary: Get alert list
 *     tags: [Alerts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [firing, acknowledged, resolved, silenced]
 *         description: Filter by alert status
 *       - in: query
 *         name: severity
 *         schema:
 *           type: string
 *           enum: [critical, warning, info]
 *         description: Filter by severity
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           default: 20
 *     responses:
 *       200:
 *         description: Alert list retrieved successfully
 *       401:
 *         description: Unauthorized
 */
router.get('/', alertController.getAlerts.bind(alertController));

/**
 * @swagger
 * /alerts/statistics:
 *   get:
 *     summary: Get alert statistics
 *     tags: [Alerts]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Alert statistics retrieved successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: number
 *                       example: 150
 *                     firing:
 *                       type: number
 *                       example: 23
 *                     acknowledged:
 *                       type: number
 *                       example: 45
 *                     resolved:
 *                       type: number
 *                       example: 78
 *                     silenced:
 *                       type: number
 *                       example: 4
 *       401:
 *         description: Unauthorized
 */
router.get('/statistics', alertController.getAlertStatistics.bind(alertController));

/**
 * @swagger
 * /alerts/{id}:
 *   get:
 *     summary: Get alert details by ID
 *     tags: [Alerts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: Alert ID
 *     responses:
 *       200:
 *         description: Alert details retrieved successfully
 *       404:
 *         description: Alert not found
 *       401:
 *         description: Unauthorized
 */
router.get('/:id', alertController.getAlert.bind(alertController));

/**
 * @swagger
 * /alerts/{id}/acknowledge:
 *   post:
 *     summary: Acknowledge an alert
 *     tags: [Alerts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: Alert ID
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               comment:
 *                 type: string
 *                 example: Investigating the issue
 *     responses:
 *       200:
 *         description: Alert acknowledged successfully
 *       404:
 *         description: Alert not found
 *       401:
 *         description: Unauthorized
 */
router.post('/:id/acknowledge', alertController.acknowledgeAlert.bind(alertController));

/**
 * @swagger
 * /alerts/{id}/resolve:
 *   post:
 *     summary: Resolve an alert
 *     tags: [Alerts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: Alert ID
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               resolution:
 *                 type: string
 *                 example: Restarted the service
 *     responses:
 *       200:
 *         description: Alert resolved successfully
 *       404:
 *         description: Alert not found
 *       401:
 *         description: Unauthorized
 */
router.post('/:id/resolve', alertController.resolveAlert.bind(alertController));

/**
 * @swagger
 * /alerts/{id}/silence:
 *   post:
 *     summary: Silence an alert
 *     tags: [Alerts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: Alert ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - duration
 *             properties:
 *               duration:
 *                 type: number
 *                 description: Silence duration in minutes
 *                 example: 30
 *               comment:
 *                 type: string
 *                 example: Maintenance window
 *     responses:
 *       200:
 *         description: Alert silenced successfully
 *       404:
 *         description: Alert not found
 *       401:
 *         description: Unauthorized
 */
router.post('/:id/silence', alertController.silenceAlert.bind(alertController));

/**
 * @swagger
 * /alerts/{id}/analyze:
 *   post:
 *     summary: AI analysis of alert
 *     tags: [Alerts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: Alert ID
 *     responses:
 *       200:
 *         description: AI analysis completed successfully
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     analysis:
 *                       type: string
 *                       example: Root cause appears to be high CPU usage due to memory leak
 *                     suggestedActions:
 *                       type: array
 *                       items:
 *                         type: string
 *                       example: ["Restart the service", "Check application logs", "Monitor memory usage"]
 *                     relatedKnowledge:
 *                       type: array
 *                       items:
 *                         type: object
 *       404:
 *         description: Alert not found
 *       401:
 *         description: Unauthorized
 */
router.post('/:id/analyze', alertController.analyzeAlertWithAI.bind(alertController));

/**
 * @swagger
 * /alerts/{alertId}/tickets:
 *   post:
 *     summary: Create ticket from alert
 *     tags: [Alerts]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: alertId
 *         required: true
 *         schema:
 *           type: string
 *         description: Alert ID
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 example: High CPU Usage on Production Server
 *               priority:
 *                 type: string
 *                 enum: [low, medium, high, critical]
 *                 example: high
 *     responses:
 *       201:
 *         description: Ticket created successfully
 *       404:
 *         description: Alert not found
 *       401:
 *         description: Unauthorized
 */
router.post('/:alertId/tickets', alertController.createTicketFromAlert.bind(alertController));

export default router;
